package com.shop2cn.iapi.prodstock.testcase;

import java.io.IOException;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.prodstock.parameter.StockInBean;
import com.shop2cn.iapi.prodstock.service.StockInCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymatou.iapi.prodstock.parameter.PreOrderReturnCatalogDto.CatalogListDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*手动入库加库存Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_StockIn{
	private static StockInBean stockinBean;
	private static StockInCall stockinCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("手动入库加库存Test");
		try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		stockinBean=new StockInBean();
		stockinCall=new StockInCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(stockinCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	private static int csId = AgentUsers.csId;
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	private static int csId1 = AgentUsers.csId1;
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_001() {
		Logger.start(true,"渠道商手动增加十个真实库存，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();
			ca.setBuyerId(rootUserId);
			ca.setStockNum(10);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));stockinBean.setCatalogList(Arrays.asList(ca));
			stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("200", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock()+10,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock()+10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_002() {
		Logger.start(true,"代理商增加渠道商的库存，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(6);
			addAgentProductBean.getCatalogList().get(0).setStock(5);
			String RootproductId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, vipAgentUserName1, RootproductId, vipRootUserId, false);
			String productId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(vipAgentUserId1, productId);
					
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();

			ca.setBuyerId(vipAgentUserId1);
			ca.setStockNum(10);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("200", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(vipAgentUserId1, productId);
					
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_003() {
		Logger.start(true,"渠道商手动增加小于0的库存，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();

			ca.setBuyerId(rootUserId);
			ca.setStockNum(-3);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("201", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_004() {
		Logger.start(true,"用户id为空，可为空成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();
			ca.setStockNum(10);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("200", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock()+10,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock()+10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_005() {
		Logger.start(true,"商品id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();

			ca.setBuyerId(rootUserId);
			ca.setStockNum(10);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(null);
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("201", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_006() {
		Logger.start(true,"规格id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();

			ca.setBuyerId(rootUserId);
			ca.setStockNum(10);
			ca.setCatalogId(null);
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("201", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_007() {
		Logger.start(true,"增加0库存，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					
			
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();

			ca.setBuyerId(rootUserId);
			ca.setStockNum(0);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("201", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StockIn_008() {
		Logger.start(true,"入库价格价格禁用状态规格，可以出库");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setAgentPriceList(null);
			addAgentProductBean.getCatalogList().get(0).setDirectPrice(null);
			addAgentProductBean.getCatalogList().get(0).setMarketPrice(0);
			
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
					System.out.println();
			
					
			stockinBean.setInventoryNo("出库单"+TestDataManager.getRandomNum(10));
			CatalogListDto ca = new CatalogListDto();
			ca.setBuyerId(rootUserId);
			ca.setStockNum(10);
			ca.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			ca.setProductId(productId);stockinBean.setCatalogList(Arrays.asList(ca));stockinBean.setCatalogList(Arrays.asList(ca));
			stockinBean.setCatalogList(Arrays.asList(ca));
			stockinCall.setData(stockinBean);
			stockinCall.callService();			
			Logger.verifyEquals("200", stockinCall.getString("Code"), "验证Code");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock()+10,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock()+10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}